package com.fick.tags;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;
import com.fick.util.General;
/**
 * 鏄剧ず鎸囧畾鏍忕洰淇℃伅
 * @author 鐜嬫旦寮�
 * 2009-08-14
 * 
 * **/
public class NewsContentTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsContentTag.class);
	//鏍忕洰id
	private int channelid=0;
	//浠庣鍑犳潯寮�
	private int beginNum=0;
		
	private String height = "175";
	
	private String width = "235";
	//鏄剧ず鍑犳潯
	private int num=10;
	//鏍囬闀垮害
	private int length=30;
	//table灞炴�
	private String tableAttr="";
	//tr灞炴�
	private String trAttr="";
	//dot td 灞炴�
	private String dotTdAttr="";
	//title td 灞炴�
	private String titleTdAttr="";
	//date td 灞炴�
	private String dateTdAttr="";
	//dot 鍥剧墖
	private String dotSrc="";
	//dot 鍥剧墖灞炴�
	private String dotAttr="";
	//<a>鏍囩href椤甸潰
	private String linkPage="";
	//<a>鏍囩灞炴�
	private String linkAttr="";
	//鏄惁鏄剧ず鏃ユ湡
	private boolean isDate=true;
	//鏃ユ湡鏍煎紡
	private String dateFormat="yyyy-MM-dd";
	//鏄惁鏄剧ず瀛愭爮鐩�
	private boolean isChild=false;
	
	//瀛愭爮鐩爣棰樺睘鎬�
	private String clikTdAttr="";
	private String spanAttr="";
	private String dotString;
	
	//鏄惁鏄剧ず鐐瑰嚮閲�
	private boolean isCliknum=false;
	
	private boolean isSign=false;   //鏄惁绛炬敹
	
	public int doStartTag() throws JspException {
			
		try {
			StringBuffer sb = new StringBuffer();
			NewsDAO dao=new NewsDAO();
			List<News> list=null;
			if(isChild){
				list=dao.findPageByHql("where isAss='1' and channelid.channelId in(select channelId from Channel where fatherId="+channelid+") order by isTop desc addDate desc", 1, beginNum+num);
			}else{
				list=dao.findPageByHql("where isAss='1' and channelid.channelId="+channelid+" order by isTop desc addDate desc", 1, beginNum+num);
			}
			
			if (list!=null && list.size()>beginNum) {
				News news;
				sb.append("<table ");
				sb.append(tableAttr);
				sb.append(" ><tbody>");
				sb.append("<tr ");
				sb.append(trAttr);
				sb.append(" >");
				sb.append("<td ");
				sb.append(dotTdAttr);
				sb.append(" >");
				sb.append("<div id='fickdemo' style='overflow:hidden;height:"+height+"px;width:"+width+"px; border:1px solid #ffffff;'>");
				sb.append("<div id='fickdemo1'>");
				sb.append("</br></br></br></br>");
				for (int i = beginNum; i < list.size(); i++) {
					news=list.get(i);
					if(!dotString.equals("")){
						sb.append("&nbsp;&nbsp;&nbsp;&nbsp;<img src='"+dotString+"' />&nbsp;");
					}
					sb.append("<a href='info.jsp?channelid="+news.getChannelid().getChannelId()+"&newsid="+news.getNewId()+"' target='_blank' ><font color='red' >"+news.getTitle()+"</font></a>");
					
					SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); 
					long day=0;
					try{
						java.util.Date date= myFormatter.parse(myFormatter.format(new Date())); 
					    java.util.Date mydate= myFormatter.parse(myFormatter.format(news.getAddDate())); 
						day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
					}catch(Exception er){er.printStackTrace();}
					if(day<=0)
					{
						sb.append("<img src='images/new.gif'>");
					}
					if(isDate){
						sb.append("&nbsp;&nbsp;&nbsp;&nbsp;<font color='red' >[ "+new SimpleDateFormat(dateFormat).format(news.getAddDate())+" ]</font><br/><br/>");
					}
					if(list.size()<3){
						sb.append("<br/><br/><br/>");
					}
				}
				sb.append("</div><div id='fickdemo2'></div></div>");
				sb.append("</td>");
				sb.append("</tr>");
				sb.append("</tbody></table>");
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		}catch(IOException e)
		{
			log.error("鏍囩鏄剧ず閿欒"+e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	public int getChannelid() {
		return channelid;
	}
	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}
	public int getBeginNum() {
		return beginNum;
	}
	public void setBeginNum(int beginNum) {
		this.beginNum = beginNum;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getTableAttr() {
		return tableAttr;
	}
	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}
	public String getTrAttr() {
		return trAttr;
	}
	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}
	public String getDotTdAttr() {
		return dotTdAttr;
	}
	public void setDotTdAttr(String dotTdAttr) {
		this.dotTdAttr = dotTdAttr;
	}
	public String getTitleTdAttr() {
		return titleTdAttr;
	}
	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}
	public String getDateTdAttr() {
		return dateTdAttr;
	}
	public void setDateTdAttr(String dateTdAttr) {
		this.dateTdAttr = dateTdAttr;
	}
	public String getDotSrc() {
		return dotSrc;
	}
	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}
	public String getDotAttr() {
		return dotAttr;
	}
	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}
	public String getLinkPage() {
		return linkPage;
	}
	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}
	public String getLinkAttr() {
		return linkAttr;
	}
	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}
	public boolean getIsDate() {
		return isDate;
	}
	public void setIsDate(boolean isDate) {
		this.isDate = isDate;
	}
	public String getDateFormat() {
		return dateFormat;
	}
	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
	public boolean getIsChild() {
		return isChild;
	}
	public void setIsChild(boolean isChild) {
		this.isChild = isChild;
	}
	public String getSpanAttr() {
		return spanAttr;
	}
	public void setSpanAttr(String spanAttr) {
		this.spanAttr = spanAttr;
	}
	public String getClikTdAttr() {
		return clikTdAttr;
	}
	public void setClikTdAttr(String clikTdAttr) {
		this.clikTdAttr = clikTdAttr;
	}
	public boolean getIsCliknum() {
		return isCliknum;
	}
	public void setIsCliknum(boolean isCliknum) {
		this.isCliknum = isCliknum;
	}
	public boolean getIsSign() {
		return isSign;
	}
	public void setIsSign(boolean isSign) {
		this.isSign = isSign;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getDotString() {
		return dotString;
	}
	public void setDotString(String dotString) {
		this.dotString = dotString;
	}
	
}
